Imports System.IO
Imports System.Net
Imports System.Text

''' <summary>
''' todo: change list to grid view
''' </summary>
''' <remarks></remarks>
Public Class DummyClient

    Private ReadOnly _AppPath As String = Application.StartupPath

    Private Sub btnAddKey_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddKey.Click

        Dim key As String = txtNewKey.Text
        lstKeys.Items.Add(key)

        btnClearKeys.Enabled = True
        ToggleMoveButtons()

        RequestDataChanged()

    End Sub

    Private Sub btnRemoveKey_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemoveKey.Click

        lstKeys.Items.Remove(lstKeys.SelectedItem)

        If lstKeys.Items.Count = 0 Then
            btnClearKeys.Enabled = False
        End If
        btnRemoveKey.Enabled = False

        ToggleMoveButtons()

        RequestDataChanged()

    End Sub

    Private Sub btnClearKeys_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearKeys.Click

        lstKeys.Items.Clear()

        btnClearKeys.Enabled = False
        btnRemoveKey.Enabled = False

        RequestDataChanged()

    End Sub

    Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click

        Dim address As String = txtAddress.Text
        'Dim address As String = "http://localhost/downloadserver/service.asmx/HelloWorld"
        Dim xml As String = txtXml.Text
        Dim uri As New Uri(address)

        Dim requestString As String = "request=" & xml
        Dim buffer() As Byte = Encoding.Unicode.GetBytes(requestString)
        'Dim t As String = Encoding.Unicode.GetString(buffer)

        Dim request As HttpWebRequest = HttpWebRequest.Create(Uri)
        request.Method = WebRequestMethods.Http.Post
        request.ContentType = "text/xml"
        request.ContentLength = buffer.Length
        request.GetRequestStream.Write(buffer, 0, buffer.Length)

        Dim response As HttpWebResponse = request.GetResponse()
        If response.StatusCode <> HttpStatusCode.OK Then
            MsgBox(response.StatusCode)
        Else
            'MsgBox(response.StatusCode)
            MsgBox(response.ContentType)
            Select Case response.ContentType
                Case "text/xml"
                Case "application/octet-stream"
                Case Else
            End Select
        End If


    End Sub

    Private Sub btnCopy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCopy.Click

        Dim txt As String = txtXml.Text

        Clipboard.Clear()
        Clipboard.SetText(txt)

    End Sub

    Private Sub RequestDataChanged()

        Dim requestXmlString As String

        RequestBuilder.Settings.OmitXmlDeclaration = chkOmitXmlDecl.Checked

        If optInformation.Checked Then
            Dim tool As String = txtServiceTool.Text
            Dim modality As String = txtModality.Text
            Dim model As String = txtModel.Text
            Dim language As String = txtLanguage.Text
            Dim sessionId As String = txtSessionId.Text
            Dim keyCount As Integer = lstKeys.Items.Count
            Dim keys(keyCount - 1) As String
            lstKeys.Items.CopyTo(keys, 0)

            ValidateInformationRequest(tool, modality, model, language, sessionId, keys)
            requestXmlString = RequestBuilder.BuildInformationRequest(tool, modality, model, language, sessionId, keys)
        ElseIf optFile.Checked Then
            Dim tool As String = txtServiceTool.Text
            Dim modality As String = txtModality.Text
            Dim model As String = txtModel.Text
            Dim identifier As String = txtIdentifier.Text
            Dim sessionId As String = txtSessionId.Text

            ValidateFileRequest(tool, modality, model, identifier, sessionId)
            requestXmlString = RequestBuilder.BuildFileRequest(tool, modality, model, identifier, sessionId)
        ElseIf optSession.Checked Then
            Dim tool As String = txtServiceTool.Text
            Dim modality As String = txtModality.Text
            Dim model As String = txtModel.Text

            ValidateSessionRequest(tool, modality, model)
            requestXmlString = RequestBuilder.BuildSessionRequest(tool, modality, model)
        Else
            requestXmlString = ""

            RequestTypeError()
        End If

        txtXml.Text = requestXmlString

    End Sub

    Private Sub RequestTypeError()

        ErrorProvider1.Clear()
        ErrorProvider1.SetError(lblRequestType, "request type expected")

    End Sub

    Private Sub ValidateInformationRequest(ByVal tool As String, ByVal modality As String, ByVal model As String, ByVal language As String, ByVal sessionId As String, ByVal keys() As String)

        Dim msg As String
        ErrorProvider1.Clear()

        msg = RequestBuilder.ValidateServiceTool(tool)
        If msg IsNot Nothing Then
            ErrorProvider1.SetError(txtServiceTool, msg)
        End If

        msg = RequestBuilder.ValidateModality(modality)
        If msg IsNot Nothing Then
            ErrorProvider1.SetError(txtModality, msg)
        End If

        msg = RequestBuilder.ValidateModel(model)
        If msg IsNot Nothing Then
            ErrorProvider1.SetError(txtModel, msg)
        End If

        msg = RequestBuilder.ValidateLanguage(language)
        If msg IsNot Nothing Then
            ErrorProvider1.SetError(txtLanguage, msg)
        End If

        msg = RequestBuilder.ValidateSessionId(sessionId)
        If msg IsNot Nothing Then
            ErrorProvider1.SetError(txtSessionId, msg)
        End If
    End Sub

    Private Sub ValidateFileRequest(ByVal tool As String, ByVal modality As String, ByVal model As String, ByVal identifier As String, ByVal sessionId As String)

        Dim msg As String
        ErrorProvider1.Clear()

        msg = RequestBuilder.ValidateServiceTool(tool)
        If msg IsNot Nothing Then
            ErrorProvider1.SetError(txtServiceTool, msg)
        End If

        msg = RequestBuilder.ValidateModality(modality)
        If msg IsNot Nothing Then
            ErrorProvider1.SetError(txtModality, msg)
        End If

        msg = RequestBuilder.ValidateModel(model)
        If msg IsNot Nothing Then
            ErrorProvider1.SetError(txtModel, msg)
        End If

        msg = RequestBuilder.ValidateIdentifier(identifier)
        If msg IsNot Nothing Then
            ErrorProvider1.SetError(txtIdentifier, msg)
        End If

        msg = RequestBuilder.ValidateSessionId(sessionId)
        If msg IsNot Nothing Then
            ErrorProvider1.SetError(txtSessionId, msg)
        End If
    End Sub

    Private Sub ValidateSessionRequest(ByVal tool As String, ByVal modality As String, ByVal model As String)

        Dim msg As String
        ErrorProvider1.Clear()

        msg = RequestBuilder.ValidateServiceTool(tool)
        If msg IsNot Nothing Then
            ErrorProvider1.SetError(txtServiceTool, msg)
        End If

        msg = RequestBuilder.ValidateModality(modality)
        If msg IsNot Nothing Then
            ErrorProvider1.SetError(txtModality, msg)
        End If

        msg = RequestBuilder.ValidateModel(model)
        If msg IsNot Nothing Then
            ErrorProvider1.SetError(txtModel, msg)
        End If
    End Sub

    Private Sub txtAddress_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAddress.TextChanged

        AddressChanged()

    End Sub

    Private Sub AddressChanged()

        Dim txt As String = txtAddress.Text
        Dim isUri As Boolean
        Try
            Dim uri As New Uri(txt)
            isUri = uri.IsAbsoluteUri
        Catch ex As Exception
            isUri = False
        End Try

        grpRequestType.Enabled = isUri
        grpXml.Enabled = isUri
        btnCalc.Enabled = isUri

    End Sub

    Private Sub grpRequestType_EnabledChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grpRequestType.EnabledChanged

        If grpRequestType.Enabled Then
            RequestDataChanged()
        Else
            grpGeneral.Enabled = False
        End If
    End Sub

    Private Sub optInformation_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optInformation.CheckedChanged

        If optInformation.Checked Then
            grpGeneral.Enabled = True
            txtServiceTool.Enabled = True
            txtModality.Enabled = True
            txtModel.Enabled = True
            txtLanguage.Enabled = True
            txtSessionId.Enabled = True
            txtIdentifier.Enabled = False

            grpKeys.Enabled = True
        Else
            grpKeys.Enabled = False
        End If

        If optInformation.Focused Then
            RequestDataChanged()
        End If
    End Sub

    Private Sub optFile_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optFile.CheckedChanged

        If optFile.Checked Then
            grpGeneral.Enabled = True
            txtServiceTool.Enabled = True
            txtModality.Enabled = True
            txtModel.Enabled = True
            txtLanguage.Enabled = False
            txtSessionId.Enabled = True
            txtIdentifier.Enabled = True
        End If

        If optFile.Focused Then
            RequestDataChanged()
        End If
    End Sub

    Private Sub optSession_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optSession.CheckedChanged

        If optSession.Checked Then
            grpGeneral.Enabled = True
            txtServiceTool.Enabled = True
            txtModality.Enabled = True
            txtModel.Enabled = True
            txtLanguage.Enabled = False
            txtSessionId.Enabled = False
            txtIdentifier.Enabled = False
        End If

        If optSession.Focused Then
            RequestDataChanged()
        End If
    End Sub

    Private Sub GeneralChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSessionId.TextChanged, txtIdentifier.TextChanged, txtLanguage.TextChanged, txtModel.TextChanged, txtModality.TextChanged, txtServiceTool.TextChanged

        If TypeOf sender Is TextBox Then
            Dim txt As TextBox = sender
            If txt.Focused Then
                RequestDataChanged()
            End If
        End If

    End Sub

    Private Sub btnMoveDown_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMoveDown.Click

        Dim obj As Object = lstKeys.SelectedItem
        Dim index As Integer = lstKeys.SelectedIndex
        lstKeys.Items(index) = lstKeys.Items(index + 1)
        lstKeys.Items(index + 1) = obj

        RequestDataChanged()

    End Sub

    Private Sub btnMoveUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMoveUp.Click

        Dim obj As Object = lstKeys.SelectedItem
        Dim index As Integer = lstKeys.SelectedIndex
        lstKeys.Items(index) = lstKeys.Items(index - 1)
        lstKeys.Items(index - 1) = obj

        RequestDataChanged()

    End Sub

    Private Sub lstKeys_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstKeys.SelectedIndexChanged

        ToggleMoveButtons()

        'If index <> -1 Then
        '    btnRemoveKey.Enabled = True
        'End If
        btnRemoveKey.Enabled = True
    End Sub

    Private Sub ToggleMoveButtons()

        btnMoveUp.Enabled = False
        btnMoveDown.Enabled = False

        Dim index As Integer = lstKeys.SelectedIndex
        If index > 0 Then
            btnMoveUp.Enabled = True
        End If
        If index >= 0 And index < lstKeys.Items.Count - 1 Then
            btnMoveDown.Enabled = True
        End If

    End Sub

    Private Sub XmlError()

        ErrorProvider1.Clear()
        'ErrorProvider1.SetIconAlignment(txtXml, ErrorIconAlignment.BottomRight)
        ErrorProvider1.SetError(txtXml, "request xml format expected")

    End Sub

    Private Sub txtXml_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtXml.TextChanged

        If txtXml.Focused Then
            Dim xml As String = txtXml.Text

            chkOmitXmlDecl.Checked = Not XmlString.HasDeclaration(xml)

            Dim r As Request
            Try
                r = Request.Parse(xml)
                If r Is Nothing Then
                    RequestTypeError()
                End If
            Catch ex As Exception
                r = Nothing
                XmlError()
            End Try
            If TypeOf r Is InformationRequest Then
                Dim ir As InformationRequest = r
                optInformation.Checked = True
                txtServiceTool.Text = ir.Tool
                txtModality.Text = ir.Modality
                txtModel.Text = ir.Model
                txtLanguage.Text = ir.Language
                'txtIdentifier.Text = ""
                txtSessionId.Text = ir.SessionId

                lstKeys.Items.Clear()
                lstKeys.Items.AddRange(ir.Keys)

                ValidateInformationRequest(ir.Tool, ir.Modality, ir.Model, ir.Language, ir.SessionId, ir.Keys)
            ElseIf TypeOf r Is FileRequest Then
                Dim fr As FileRequest = r
                optFile.Checked = True
                txtServiceTool.Text = fr.Tool
                txtModality.Text = fr.Modality
                txtModel.Text = fr.Model
                'txtLanguage.Text = ""
                txtIdentifier.Text = fr.Identifier
                txtSessionId.Text = fr.SessionId

                ValidateFileRequest(fr.Tool, fr.Modality, fr.Model, fr.Identifier, fr.SessionId)
            ElseIf TypeOf r Is SessionRequest Then
                Dim sr As SessionRequest = r
                optSession.Checked = True
                txtServiceTool.Text = sr.Tool
                txtModality.Text = sr.Modality
                txtModel.Text = sr.Model
                'txtLanguage.Text = ""
                'txtIdentifier.Text = ""
                'txtSessionId.Text = ""

                ValidateSessionRequest(sr.Tool, sr.Modality, sr.Model)
            Else
                optInformation.Checked = False
                optFile.Checked = False
                optSession.Checked = False

                grpGeneral.Enabled = False

                'XmlError()
                'RequestTypeError()
            End If
        End If

        btnCopy.Enabled = txtXml.Text.Length > 0

    End Sub

    Private Sub chkOmitXmlDecl_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkOmitXmlDecl.CheckedChanged

        If chkOmitXmlDecl.Focused Then
            RequestDataChanged()
        End If
    End Sub

    Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click

        Dim obj As Object = CreateObject("InternetExplorer.Application")
        obj.visible = True

    End Sub

    Private Sub DummyClient_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        txtAddress.Text = "http://localhost/downloadserver/downloadservice.asmx/Download"
        AddressChanged()

    End Sub
End Class
